package component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import operate.DictOperate;
import operate.vo.Dict;

public class FormConstants {

	public static final String TEXT = "text";
	public static final String PASSWORD = "password";
	public static final String IMG = "img";
	public static final String File = "file";
	public static final String RADIO = "radio";
	public static final String CHECKBOX = "checkbox";
	public static final String SELECT = "select";
	public static final String DATE = "date";
	public static final String TEXTAREA = "textarea";
	
	public static String construt(Map<String, String> map, int formType) {
		String type = (String) map.get("TYPE");
		String id = (map.get("ID"));
		String reg = map.get("REG");
		String tip = map.get("TIP");
		String clazz = map.get("CLAZZ");
		String value = map.get("VALUE");
		String valueList = map.get("VALUE_LIST");
		String defValue = map.get("DEFAULT_VALUE");
		String temp = "";
		String result="";
		if (formType == 1) {
			if(defValue!=null){
				temp = defValue;
			}
			
		} else {
			if(value!=null){
				  temp=value;
			}
		}
		if (TEXT.equals(type)) {
			return new Text(id, id, temp, clazz, reg, tip).toString();
		}
		if (PASSWORD.equals(type)) {
			return new Password(id, id, temp, clazz, reg, tip).toString();
		}
		if (TEXTAREA.equals(type)) {
			return new Textarea(id, id, temp, clazz, reg, tip).toString();
		}
		if (DATE.equals(type)) {
			String onfocus = "WdatePicker({skin:'whyGreen',maxDate:'%y-%M-%d'})";
			return new Text(id, id, temp, "Wdate", reg, tip).toString();
		}
		if (IMG.equals(type)) {

		}
		if (File.equals(type)) {

		}
		if (RADIO.equals(type)) {
			List<Dict> dicts = DictOperate.getDictBycode(valueList);
			StringBuffer radios = new StringBuffer();
			for (Dict dict : dicts) {
				if (temp!= null && temp.equals(dict.getValue())) {
					result = "checked";
				} else {
					result = null;
				}
				radios.append(new Radio(id, id, dict.getName(), result, null,null, dict.getValue()));
			}
			return radios.toString();
		}
		if (CHECKBOX.equals(type)) {
			String[] values = temp.split(":");
			List<Dict> dicts = DictOperate.getDictBycode(valueList);
			StringBuffer checkboxs = new StringBuffer();
			for (Dict dict : dicts) {
				for (String val : values) {
					if (val != null && val.equals(dict.getValue())) {
						result = "checked";
					} else {
						result = null;
					}
				}
				checkboxs.append(new Checkbox(id, id, dict.getName(), result,null, null, dict.getValue()).toString());
			}
			return checkboxs.toString();
		}
		if (SELECT.equals(type)) {
			List<Dict> dicts = DictOperate.getDictBycode(valueList);
			List<String> options = new ArrayList<String>();
			for (Dict dict : dicts) {
				if(temp!=null&&temp.equals(dict.getValue())){
					result="selected";
				}else{
					result=null;
				}
				Option option = new Option(dict.getValue(), result, dict
						.getName());
				options.add(option.toString());
			}
			Select select = new Select(id, id, reg, tip, options);
			return select.toString();
		}

		return null;
	}

	public static void main(String[] args) {
		String str = "";
		String[] strs = str.split(";");
		System.out.println("ddd:" + strs[0]);
	}

}
